<?
/* class trip.php
 * The class for interacting with the trips database
*/

include('data/lib_f_pictures.inc.php'); 
include('data/lib_components.inc.php');

    /**
    * @desc Returns the HTML for a regional drop down box with a region pre-selected
    */
    function getRegionDropBox($selectedRegion=14)
    {
        $regions = mq("SELECT id, name FROM `regions`");
        
        $num_regions = mqs("SELECT COUNT(*) FROM `regions`");
        
        $res .= '<select name="region">';
        
        for($i=1;$i<=$num_regions;$i++)
        {
            $region = mysql_fetch_assoc($regions);
            extract($region);
            $res .= '<option value="'.$id.'" '.($selectedRegion==$id ? ' selected="selected" ':'').' >'.$name.'</option>';
        }
        $res .= '</select>';
        return $res;
    }
        

class Trip
{
    
	 function getPostBox($id)
     {
        //returns HTML for the post-box
        // Check if user may edit or only look
        $edit = $_SESSION['logged'];
        if ($edit)
        {
            $res=  "<div class=\"post-box\">\n
                <form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\" >\n
                <textarea name=\"post\" cols=40 rows=5></textarea><br /> \n
                <input type=\"hidden\" name=\"tripid\" value=\"".$id."\" /> \n
                <input type=\"submit\" value=\"Post\"/> \n
                </form> \n
                </div> \n";
        }
        else
        {
            $res ="Login to post a comment.<br />";
        }
        return $res;    
    }


    
    /**
    * @desc 
    */
    function fetchAddTrip()
    {
        $res = '<div class="trip-add"><h2>Add a New Trip into the database</h2>
        <form method="post" action="'.$_SERVER['PHP_SELF'].'" >
        Trip Name: <input type="text" name="name"><br>
        Short Description: <input type="text" name="short_d"><br>
        Long Description: <br><textarea rows="5" cols="40" name="long_d" ></textarea><br>
        Region: '.getRegionDropBox(isset($_GET['region'])?$_GET['region']:'14').'<br>
        Map: '. PicUploadForm($map,'map','newmap',TRIPMAPDIR,50).'<br>                    
        Photos : Mmmm this will take a while. Guess who doesn\'t have a while right now!!!<br>
        Average Time to Complete Run: <input type="text" name="time"><br>
        Grade: <input type="text" maxlength="20" name="time"><br>
        <input type="button" value="Submit" name="newtrip">
        </form>
        </div>
        ';
        return $res;
    }
    
    
    /**
    * @desc Add a new trip into the database.
    */
    function newTrip($name, $short_d, $long_d, $region='12', $map = "",$photos = "" ,$time = '2 hours', $grade)
    {
        if($id = mq("INSERT INTO `uccc_db`.`trips` (`region` ,`map` ,`photos` ,`name` ,`short_d` ,`long_d` ,`grade` ,`time` ,`distance` ,`updated`)
                    VALUES ('$region', '$map', '$photos', '$name', '$short_d', '$long_d', '$grade', '$time', '$distance', NOW( ) )"))
        {
            $content .= "Inserted new trip ($name) ";
            logevent("Trip #$id - $name inserted into trips database by ". mqs("SELECT Name FROM `members` WHERE MID = ".$_SESSION['MID']) );
        }
    }    
	
    /**
	 * @desc Returns html data for a trip.
	 */
	 function fetchByTripId($id)
	 {
        if($data = mqs("SELECT * FROM `trips` WHERE `id` = $id"))
		{	
			extract($data);
            $str = "<h1>Trip Details: ".stripslashes($name).'</h1>
                <div class="trip-box">
                    <div class="trip-box-grade">'.$grade.'</div>
                    <div class="trip-box-short_d">'.stripslashes($short_d).'</div>
                    <div class="trip-box-long_d">'.stripslashes($long_d)."</div> 
                </div>";
		} else { $str .= "No Trip with id: $id"; }
        return $str;
    }
	
	/*
	* Returns html data for the trips in a region
	*/
	function fetchByRegionId($id)
	{
        $q = "SELECT * FROM `trips` WHERE region = $id";
        $region = mqs('SELECT name FROM `regions` WHERE id = "'.$id.'"');
		$map = mqs('SELECT map FROM `regions` WHERE id = "'.$id.'"');
        $result = mq($q);
        if (!$result || (mysql_numrows($result) < 1)) 
		{
            return false;
        }
        $str = "<h2>Trips by Area: <b>".stripslashes($region)."</b></h2>";
		
        $dbarray = mysql_fetch_array($result);
        while ($dbarray)
		{
            $str = $str.
                "<div class=\"trip-box\"> \n"
                ."<a href=\"uccc.php?topic=trips&tripid=".$dbarray['id']."\">".stripslashes($dbarray['name'])."</a><br />  <i>"
                .stripslashes($dbarray['short_d'])."</i> <br /> Grade: ".stripslashes($dbarray['grade'])."
                </div>";
            $dbarray = mysql_fetch_array($result);
        }
        $str .= ($map) ? '<div class="trip-region-map"><img src="'.$map.'"/></div>' : "";
        return $str;
    }
	
	/*
	* Returns html data for the trips in a grade
	*/
	function fetchByGrade($grade)
	{
        $q = "SELECT * FROM `trips` WHERE grade = $grade";
        $result = mq($q);
        if (!$result || (mysql_numrows($result) < 1)) 
		{
            return false;
        }
        $str = "<h1>Trips rated grade: ".stripslashes($grade)."</h1>";
        $dbarray = mysql_fetch_array($result);
        while ($dbarray)
		{
            $str = $str.
                "<div class=\"trip-box\"> \n"
                ."<a href=\"uccc.php?topic=trips&tripid=".$dbarray['id']."\">".stripslashes($dbarray['name'])."</a><br /> \n"
                .stripslashes($dbarray['short_d'])." \n Region:".stripslashes($dbarray['region'])."
                </div>";
            $dbarray = mysql_fetch_array($result);
        }
        return $str;
    }
	
    function fetchPostData($id)
    {
    }   
    
	function fetchRegions()
	{
        //returns html data for a trip
        $str = <<<EOT
		<img usemap="#newzealand" src="data/regions.jpg" style="border:solid 1px black"/>
         <map name="newzealand">
                <area title="Northland" alt="" shape="poly" coords="291,136,285,123,254,80,254,70,241,52,238,46,243,37,252,36,260,33,269,39,267,47,274,50,282,57,295,58,301,65,312,66,316,78,327,83,328,96,325,108,303,126,290,133"  href="uccc.php?topic=trips&region=1" /> 
                <area title="Auckland" alt="" shape="poly" coords="290,135,300,148,304,162,310,171,341,160,335,137,337,130,352,130,360,123,356,119,355,115,347,107,342,105,328,99,326,108,306,127,292,134"  href="uccc.php?topic=trips&region=2" /> 
                <area title="Waikato" alt="" shape="poly" coords="307,169,311,188,305,192,303,198,307,203,309,209,307,211,305,229,322,231,325,225,342,221,346,219,346,229,348,238,347,253,352,254,359,249,362,248,368,237,368,230,375,225,377,216,371,209,366,207,355,180,374,155,370,147,369,140,362,125,350,128,339,129,334,132,342,159,308,169"  href="uccc.php?topic=trips&region=3" /> 
                <area title="Bay Of Plenty" alt="" shape="poly" coords="373,155,355,178,363,206,375,214,370,236,375,241,376,234,382,235,383,231,391,229,399,225,405,219,409,216,409,211,422,202,429,192,429,185,434,185,433,174,410,187,410,173,401,170,394,171,393,180,389,182,383,175,380,166,375,156"  href="uccc.php?topic=trips&region=4" /> 
                <area title="Gisborne" alt="" shape="poly" coords="433,176,432,184,429,188,431,196,424,205,420,201,416,207,413,212,405,219,401,225,408,231,421,242,433,243,432,240,449,228,450,205,457,195,457,186,442,175,434,175"  href="uccc.php?topic=trips&region=5" /> 
                <area title="Hawke's Bay" alt="" shape="poly" coords="361,248,365,272,363,286,367,289,372,302,387,306,396,295,396,287,401,285,401,257,411,258,414,264,421,266,432,263,434,251,434,245,417,240,408,228,403,227,394,229,383,230,383,237,378,234,376,239,373,243,367,237,363,246"  href="uccc.php?topic=trips&region=6" /> 
                <area title="Taranaki" alt="" shape="poly" coords="321,232,318,249,323,258,327,268,320,274,318,287,307,283,298,274,287,272,278,260,277,251,277,243,293,235,308,232,318,230"  href="uccc.php?topic=trips&region=7" /> 
                <area title="Manawatu-Wanganui" alt="" shape="poly" coords="316,287,318,277,323,272,324,262,316,251,319,229,336,226,345,221,346,236,346,253,356,253,358,247,363,248,363,284,370,293,375,304,387,306,375,320,365,311,352,315,348,314,335,316,318,309,322,292,316,287"  href="uccc.php?topic=trips&region=8" /> 
                <area title="Wellington" alt="" shape="poly" coords="320,312,315,315,312,321,306,345,322,355,329,362,344,358,358,350,366,341,375,319,365,314,352,317,349,314,338,314,327,313,322,310"  href="uccc.php?topic=trips&region=9" /> 
                <area title="Nelson" alt="" shape="poly" coords="268,321,267,340,269,345,278,333,277,321,271,320,"  href="uccc.php?topic=trips&region=10" /> 
                <area title="Tasman" alt="" shape="poly" coords="269,320,265,339,269,345,249,373,240,383,237,374,229,368,229,360,247,336,240,327,229,312,246,295,259,299,269,302,271,317"  href="uccc.php?topic=trips&region=11" /> 
                <area title="Marlborough" alt="" shape="poly" coords="276,322,280,330,269,345,248,375,259,388,265,387,265,383,273,377,272,372,284,365,292,367,299,374,311,360,307,349,311,323,303,318,299,308,292,303,285,306,279,311,276,317"  href="uccc.php?topic=trips&region=12" /> 
                <area title="West Coast" alt="" shape="poly" coords="225,311,247,335,231,363,230,370,241,383,221,409,207,411,193,423,170,438,157,459,142,462,139,462,135,466,130,476,114,480,115,472,113,471,109,474,101,465,110,455,129,444,140,439,151,432,167,415,183,403,194,386,201,354,217,349,221,336,221,320,228,312"  href="uccc.php?topic=trips&region=13" /> 
                <area title="Canterbury" alt="" shape="poly" coords="210,499,189,495,180,502,180,496,171,497,165,484,160,485,157,483,152,477,154,468,157,459,168,445,168,442,176,437,197,422,204,411,219,409,240,384,247,376,260,390,266,389,265,383,273,381,274,373,286,369,298,375,289,395,282,396,278,413,271,420,261,424,261,428,270,436,271,448,267,454,257,460,247,456,231,463,215,473,212,482,210,498" href="uccc.php?topic=trips&region=14" /> 
                <area title="Otago" alt="" shape="poly" coords="209,500,188,495,179,501,179,498,171,498,167,486,160,486,155,477,157,457,147,460,139,461,132,474,112,479,112,491,116,501,117,516,131,516,133,518,141,516,142,518,139,531,152,580,168,572,172,565,173,561,183,551,198,543,199,533,198,527,210,500"  href="uccc.php?topic=trips&region=15" /> 
                <area title="Southland" alt="" shape="poly" coords="152,582,139,533,143,520,138,516,133,520,130,513,119,514,116,505,110,499,115,481,115,475,112,472,107,474,103,468,83,489,73,496,57,526,55,533,54,551,60,561,68,563,77,582,85,568,95,570,93,589,84,599,90,607,99,613,109,620,118,611,118,606,126,600,127,590,139,582,148,581"  href="uccc.php?topic=trips&region=16" /> 
         </map>
EOT;
        return $str;
    }
};

$trip = new Trip();

?>